import { Component } from "@angular/core";
import {AlertController, NavParams, LoadingController, ToastController} from "ionic-angular";
import { NavController } from "ionic-angular";
import { HomePage } from "../home/home";
import { HttpService } from "../../providers/http.service";
import { CommonFunc } from "../../providers/commonFunc";
import * as moment from "moment";
import { Storage } from "@ionic/storage";
import { appConfig } from "../../providers/config";
import { TractionPage } from "../maince-type/traction/traction";
import { EscalatorPage } from "../maince-type/escalator/escalator";
import { HydpressPage } from "../maince-type/hydpress/hydpress";
import { DebrisPage } from "../maince-type/debris/debris";
/**
 * 工作总结
 */
@Component({
  templateUrl: "sign.html"
})
export class SignPage {
  private param: any = {};
  private ElevaType: any;
  private maintenanceUserSignId1: any;
  private maintenanceUserSignId2: any;
  private suggest: string;
  private signatrue: string = new Date().getSeconds().toString();
  showDetailMaint: boolean = false;
  maitType: any;
  summary: any;
  changeInfo: any;
  safeUserSignId2: any;
  elevatorNum: any;
  maintUser2: any;
  private time1 = new Date().getTime() + "1";
  private time2 = new Date().getTime() + "2";
  userList: any = [];
  userId: any;
  endTime: any;
  constructor(
    public navCtrl: NavController,
    private alertCtrl: AlertController,
    private navParams: NavParams,
    private storage: Storage,
    private httpService: HttpService,
    private commonFunc: CommonFunc,
    private loading: LoadingController,
    private toastCtrl: ToastController,
  ) {
    this.param = this.navParams.get("params");
    this.ElevaType = this.navParams.get("ElevaType");
    this.elevatorNum = this.navParams.get("elevatorNum");

    this.storage.get('user').then(user => {
      this.userId = user.id;
      let param = {
        partyId: user.partyId,
        currentPage: 1,
        pageSize: 99
      }
      this.httpService.getUserList(param).subscribe(data => {
        this.userList = data.data.list;
        console.log(this.userList);
        //第二维保人员不能选择自己且只能是维保人员
          if (this.userList != null && this.userList.length > 0) {
              for (let i = 0; i < this.userList.length; i++) {
                  if (this.userList[i].id == this.userId) {
                    this.userList.splice(i,1);
                  }
                  if (this.userList[i].cardType != null && this.userList[i].cardType != "" && this.userList[i].cardType != undefined) {
                      if (this.userList[i].cardType.indexOf("T") == -1) {
                        this.userList.splice(i,1);
                      }
                  }else {
                      this.userList.splice(i,1);
                  }
              }
          }
      })

    });


    if (this.ElevaType == "notice") {
      this.showDetailMaint = true;
      this.getTips();
      let elevatorType = this.param.elevatoriType;
      if (elevatorType == "1" || elevatorType == "5") {
        this.httpService
          .queryTractionDetails(this.param.elevatoriMonId)
          .subscribe(dd => {
            this.elevatorNum = dd.data.traction.tractionHalfMon.elevatorNum;
            if (dd.data.traction.tractionHalfMon.type * 1 >= 1) {
              this.storage.set(this.elevatorNum + '-his-' +
                "traction-half",
                this.commonFunc.convertBean(dd.data.traction.tractionHalfMon)
              );
              this.summary = dd.data.traction.tractionHalfMon.summary;
              this.changeInfo = dd.data.traction.tractionHalfMon.changeInfo;
              this.safeUserSignId2 =
                dd.data.traction.tractionHalfMon.safeUserSignId2;
              this.suggest = dd.data.traction.tractionHalfMon.usedPartyOpt;
            }
            if (dd.data.traction.tractionHalfMon.type * 1 > 1) {
              this.storage.set(this.elevatorNum + '-his-' +
                "traction-quarter",
                this.commonFunc.convertBean(dd.data.traction.tractionQuarter)
              );
            }
            if (dd.data.traction.tractionHalfMon.type * 1 > 2) {
              this.storage.set(this.elevatorNum + '-his-' +
                "traction-half-year",
                this.commonFunc.convertBean(dd.data.traction.tractionHalfYear)
              );
            }
            if (dd.data.traction.tractionHalfMon.type * 1 > 3) {
              this.storage.set(this.elevatorNum + '-his-' +
                "traction-year",
                this.commonFunc.convertBean(dd.data.traction.tractionYear)
              );
            }

            this.maitType = dd.data.traction.tractionHalfMon.type;
          });
      }

      if (elevatorType == "2") {
        this.httpService
          .queryHydpressDetails(this.param.elevatoriMonId)
          .subscribe(dd => {
            this.elevatorNum = dd.data.hydpress.hydpressHalfMon.elevatorNum;
            if (dd.data.hydpress.hydpressHalfMon.type * 1 >= 1) {
              this.storage.set(this.elevatorNum + '-his-' +
                "hydpress-half",
                this.commonFunc.convertBean(dd.data.hydpress.hydpressHalfMon)
              );
              this.summary = dd.data.hydpress.hydpressHalfMon.summary;
              this.changeInfo = dd.data.hydpress.hydpressHalfMon.changeInfo;
              this.safeUserSignId2 =
                dd.data.hydpress.hydpressHalfMon.safeUserSignId2;
              this.suggest = dd.data.hydpress.hydpressHalfMon.usedPartyOpt;
            }
            if (dd.data.hydpress.hydpressHalfMon.type * 1 > 1) {
              this.storage.set(this.elevatorNum + '-his-' +
                "hydpress-quarter",
                this.commonFunc.convertBean(dd.data.hydpress.hydpressQuarter)
              );
            }
            if (dd.data.hydpress.hydpressHalfMon.type * 1 > 2) {
              this.storage.set(this.elevatorNum + '-his-' +
                "hydpress-half-year",
                this.commonFunc.convertBean(dd.data.hydpress.hydpressHalfYear)
              );
            }
            if (dd.data.hydpress.hydpressHalfMon.type * 1 > 3) {
              this.storage.set(this.elevatorNum + '-his-' +
                "hydpress-year",
                this.commonFunc.convertBean(dd.data.hydpress.hydpressYear)
              );
            }

            this.maitType = dd.data.hydpress.hydpressHalfMon.type;
          });
      }
      if (elevatorType == "3") {
        this.httpService
          .queryDebrisDetails(this.param.elevatoriMonId)
          .subscribe(dd => {
            this.elevatorNum = dd.data.debris.debrisHalfMon.elevatorNum;
            if (dd.data.debris.debrisHalfMon.type * 1 >= 1) {
              this.storage.set(this.elevatorNum + '-his-' +
                "debris-half",
                this.commonFunc.convertBean(dd.data.debris.debrisHalfMon)
              );
              this.summary = dd.data.debris.debrisHalfMon.summary;
              this.changeInfo = dd.data.debris.debrisHalfMon.changeInfo;
              this.safeUserSignId2 =
                dd.data.debris.debrisHalfMon.safeUserSignId2;
              this.suggest = dd.data.debris.debrisHalfMon.usedPartyOpt;
            }
            if (dd.data.debris.debrisHalfMon.type * 1 > 1) {
              this.storage.set(this.elevatorNum + '-his-' +
                "debris-quarter",
                this.commonFunc.convertBean(dd.data.debris.debrisQuarter)
              );
            }
            if (dd.data.debris.debrisHalfMon.type * 1 > 2) {
              this.storage.set(this.elevatorNum + '-his-' +
                "debris-half-year",
                this.commonFunc.convertBean(dd.data.debris.debrisHalfYear)
              );
            }
            if (dd.data.debris.debrisHalfMon.type * 1 > 3) {
              this.storage.set(this.elevatorNum + '-his-' +
                "debris-year",
                this.commonFunc.convertBean(dd.data.debris.debrisYear)
              );
            }

            this.maitType = dd.data.debris.debrisHalfMon.type;
          });
      }

      if (elevatorType == "4" || elevatorType == "6") {
        this.httpService
          .queryEscalatorDetails(this.param.elevatoriMonId)
          .subscribe(dd => {
            this.elevatorNum = dd.data.escalator.escalatorHalfMon.elevatorNum;
            if (dd.data.escalator.escalatorHalfMon.type * 1 >= 1) {
              this.storage.set(this.elevatorNum + '-his-' +
                "escalator-half",
                this.commonFunc.convertBean(dd.data.escalator.escalatorHalfMon)
              );
              this.summary = dd.data.escalator.escalatorHalfMon.summary;
              this.changeInfo = dd.data.escalator.escalatorHalfMon.changeInfo;
              this.safeUserSignId2 =
                dd.data.escalator.escalatorHalfMon.safeUserSignId2;
              this.suggest = dd.data.escalator.escalatorHalfMon.usedPartyOpt;
            }
            if (dd.data.escalator.escalatorHalfMon.type * 1 > 1) {
              this.storage.set(this.elevatorNum + '-his-' +
                "escalator-quarter",
                this.commonFunc.convertBean(dd.data.escalator.escalatorQuarter)
              );
            }
            if (dd.data.escalator.escalatorHalfMon.type * 1 > 2) {
              this.storage.set(this.elevatorNum + '-his-' +
                "escalator-half-year",
                this.commonFunc.convertBean(dd.data.escalator.escalatorHalfYear)
              );
            }
            if (dd.data.escalator.escalatorHalfMon.type * 1 > 3) {
              this.storage.set(this.elevatorNum + '-his-' +
                "escalator-year",
                this.commonFunc.convertBean(dd.data.escalator.escalatorYear)
              );
            }

            this.maitType = dd.data.escalator.escalatorHalfMon.type;
          });
      }
    }
  }

  check() {
    if (this.maintUser2 == null) {
      const alert2 = this.alertCtrl.create({
        // title: '',
        subTitle: "请选择第二维保人员！",
        buttons: ["确定"]
      });
      alert2.present();
      return false;
    }
    if (!this.maintenanceUserSignId2) {
      const alert2 = this.alertCtrl.create({
        // title: '',
        subTitle: "请签名并保存！",
        buttons: ["确定"]
      });
      alert2.present();
      return false;
    }
    return true;
  }

  //提交
  doConfirm() {
    if (!this.commonFunc.hasNetWork()) {
      const alert1 = this.alertCtrl.create({
        title: "警告",
        subTitle: "当前无网络，请确认连接后再提交",
        buttons: ["确定"]
      });
      alert1.present();
      return;
    }
    if (!this.maintenanceUserSignId1) {
      const alert2 = this.alertCtrl.create({
        // title: '',
        subTitle: "请签名并保存！",
        buttons: ["确定"]
      });
      alert2.present();
      return;
    }
      if (!(this.ElevaType=='daily'||this.ElevaType=='notice')) {
          if (!this.maintenanceUserSignId2) {
              console.log("maintenanceUserSignId2");
              const alert2 = this.alertCtrl.create({
                  // title: '',
                  subTitle: "请签名并保存！",
                  buttons: ["确定"]
              });
              alert2.present();
              return;
          }
      }
    let load = this.loading.create({
      spinner: appConfig.spinner,
      duration: appConfig.timeout
    });
    load.present();

    this.signatrue = new Date().getSeconds().toString();
    const alert = this.alertCtrl.create({
      // title: '',
      subTitle: "提交成功",
      buttons: ["确定"]
    });

    if (
      this.navParams.get("ElevaType") == "1" ||
      this.navParams.get("ElevaType") == "5"
    ) {
      this.getTips();
      if (!this.check()) {
        load.dismiss();
        return;
      }
      this.param["tractionHalfMon"].maintenanceUserId2 = this.maintUser2;
      if (this.userList) {
        this.userList.forEach(element => {
          if (element.id = this.maintUser2) {
            this.param["tractionHalfMon"].maintenanceUserName2 = element.userName;
          }
        });
      }
      this.endTime = this.param["tractionHalfMon"].maintenanceEndDate;

      this.param[
        "tractionHalfMon"
      ].maintenanceUserSignId1 = this.maintenanceUserSignId1;
      this.param[
        "tractionHalfMon"
      ].maintenanceUserSignId2 = this.maintenanceUserSignId2;

      this.httpService.addTraction(this.param).subscribe(data => {
        if (data.status == 200) {

          this.storage.set(this.elevatorNum + "traction-half", null);
          this.storage.set(this.elevatorNum + "traction-half-year", null);
          this.storage.set(this.elevatorNum + "traction-quarter", null);
          this.storage.set(this.elevatorNum + "traction-year", null);
          this.storage.set(this.elevatorNum + "work-sum",null);
          this.storage.set(this.elevatorNum + "safeImg",null);

          load.dismiss();
          this.storage.get(this.userId + 'cache').then(datalist2 => {
            if (datalist2) {
              let datalist: [any] = Object.assign([], datalist2);
              for (let i = 0; i < datalist.length; i++) {
                let element = datalist[i]
                if (element.dateTime == this.endTime) {
                  datalist.splice(i, 1);
                }
              }
              this.storage.set(this.userId + 'cache', datalist);
            }
          });
          this.navCtrl.setRoot(HomePage).then(() => {
            window["jQuery"]("#signatureid").remove();
            window["jQuery"]("#showImage").remove();
            window["jQuery"](`#` + this.time1).remove();
            window["jQuery"](`#` + this.time2).remove();
            //场景埋点
            if (data.data && data.data.status == 1) {
              this.storage.get(data.data.ext2 + this.userId + this.elevatorNum).then(dt => {
                let shownum: number = 0;
                if (dt) {
                  shownum = dt.shownum;
                }

                if (data.data.eventAction == 1) { //nothing to do
                  alert.present();
                  this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                }
                if (data.data.eventAction == 2) {
                  if (data.data.ext1 > shownum) {
                    let totst = this.commonFunc.showToast({
                      message: "恭喜您获得：" + data.data.thing + " 分",
                      duration: 3000,
                      position: "middle"
                    });
                    totst.present();
                    this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                  } else {
                    alert.present();
                  }

                }
                if (data.data.eventAction == 3) {
                  if (data.data.ext1 > shownum) {
                    this.commonFunc.openUrl(data.data.thing);
                    this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                  } else {
                    alert.present();
                  }
                }
                if (data.data.eventAction == 4) {
                  if (data.data.ext1 > shownum) {
                    this.navCtrl.push(data.data.thing, {
                      themeData: data.data,
                      elevatorNum: this.elevatorNum
                    });
                  } else {
                    alert.present();
                  }
                }
              }, error => {
                alert.present();
              })

            } else {
              alert.present();
            }
          });
        } else {
          load.dismiss();
          let totst = this.commonFunc.showToast({
            message: "提交失败",
            duration: 2000,
            position: "top"
          });
          totst.present();
        }
      });
    }
    if (this.navParams.get("ElevaType") == "2") {
      this.getTips();
      this.param["hydpressHalfMon"].maintenanceUserId2 = this.maintUser2;
      if (this.userList) {
        this.userList.forEach(element => {
          if (element.id = this.maintUser2) {
            this.param["hydpressHalfMon"].maintenanceUserName2 = element.userName;
          }
        });
      }
      this.endTime = this.param["hydpressHalfMon"].maintenanceEndDate;
      this.param[
        "hydpressHalfMon"
      ].maintenanceUserSignId1 = this.maintenanceUserSignId1;
      this.param[
        "hydpressHalfMon"
      ].maintenanceUserSignId2 = this.maintenanceUserSignId2;
      this.httpService.addHydpress(this.param).subscribe(data => {
        if (data.status == 200) {
          this.storage.set(this.elevatorNum + "hydpress-half", null);
          this.storage.set(this.elevatorNum + "hydpress-half-year", null);
          this.storage.set(this.elevatorNum + "hydpress-quarter", null);
          this.storage.set(this.elevatorNum + "hydpress-year", null);
          this.storage.set(this.elevatorNum + "work-sum",null);
          this.storage.set(this.elevatorNum + "safeImg",null);
          load.dismiss();
          this.storage.get(this.userId + 'cache').then(datalist2 => {
            if (datalist2) {
              let datalist: [any] = Object.assign([], datalist2);
              for (let i = 0; i < datalist.length; i++) {
                let element = datalist[i]
                if (element.dateTime == this.endTime) {
                  datalist.splice(i, 1);
                }
              }
              this.storage.set(this.userId + 'cache', datalist);
            }
          });
          this.navCtrl.setRoot(HomePage).then(() => {
            window["jQuery"]("#signatureid").remove();
            window["jQuery"]("#showImage").remove();
            window["jQuery"](`#` + this.time1).remove();
            window["jQuery"](`#` + this.time2).remove();
            //场景埋点
            if (data.data && data.data.status == 1) {
              this.storage.get(data.data.ext2 + this.userId + this.elevatorNum).then(dt => {
                let shownum: number = 0;
                if (dt) {
                  shownum = dt.shownum;
                }

                if (data.data.eventAction == 1) { //nothing to do
                  alert.present();
                  this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                }
                if (data.data.eventAction == 2) {
                  if (data.data.ext1 > shownum) {
                    let totst = this.commonFunc.showToast({
                      message: "恭喜您获得：" + data.data.thing + " 分",
                      duration: 3000,
                      position: "middle"
                    });
                    totst.present();
                    this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                  } else {
                    alert.present();
                  }

                }
                if (data.data.eventAction == 3) {
                  if (data.data.ext1 > shownum) {
                    this.commonFunc.openUrl(data.data.thing);
                    this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                  } else {
                    alert.present();
                  }
                }
                if (data.data.eventAction == 4) {
                  if (data.data.ext1 > shownum) {
                    this.navCtrl.push(data.data.thing, {
                      themeData: data.data,
                      elevatorNum: this.elevatorNum
                    });
                  } else {
                    alert.present();
                  }
                }
              }, error => {
                alert.present();
              })
            } else {
              alert.present();
            }
          });
        } else {
          load.dismiss();
          let totst = this.commonFunc.showToast({
            message: "提交失败",
            duration: 2000,
            position: "top"
          });
          totst.present();
        }
      });
    }
    if (this.navParams.get("ElevaType") == "3") {
      this.getTips();
      this.param["debrisHalfMon"].maintenanceUserId2 = this.maintUser2;
      if (this.userList) {
        this.userList.forEach(element => {
          if (element.id = this.maintUser2) {
            this.param["debrisHalfMon"].maintenanceUserName2 = element.userName;
          }
        });
      }
      this.endTime = this.param["debrisHalfMon"].maintenanceEndDate;
      this.param[
        "debrisHalfMon"
      ].maintenanceUserSignId1 = this.maintenanceUserSignId1;
      this.param[
        "debrisHalfMon"
      ].maintenanceUserSignId2 = this.maintenanceUserSignId2;

      this.httpService.addDebris(this.param).subscribe(data => {
        if (data.status == 200) {
          load.dismiss();
          this.storage.set(this.elevatorNum + "debris-half", null);
          this.storage.set(this.elevatorNum + "debris-half-year", null);
          this.storage.set(this.elevatorNum + "debris-quarter", null);
          this.storage.set(this.elevatorNum + "debris-year", null);
          this.storage.set(this.elevatorNum + "work-sum",null);
          this.storage.set(this.elevatorNum + "safeImg",null);
          this.storage.get(this.userId + 'cache').then(datalist2 => {
            if (datalist2) {
              let datalist: [any] = Object.assign([], datalist2);
              for (let i = 0; i < datalist.length; i++) {
                let element = datalist[i]
                if (element.dateTime == this.endTime) {
                  datalist.splice(i, 1);
                }
              }
              this.storage.set(this.userId + 'cache', datalist);
            }
          });
          this.navCtrl.setRoot(HomePage).then(() => {
            window["jQuery"]("#signatureid").remove();
            window["jQuery"]("#showImage").remove();
            window["jQuery"](`#` + this.time1).remove();
            window["jQuery"](`#` + this.time2).remove();
            //场景埋点
            if (data.data && data.data.status == 1) {
              this.storage.get(data.data.ext2 + this.userId + this.elevatorNum).then(dt => {
                let shownum: number = 0;
                if (dt) {
                  shownum = dt.shownum;
                }

                if (data.data.eventAction == 1) { //nothing to do
                  alert.present();
                  this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                }
                if (data.data.eventAction == 2) {
                  if (data.data.ext1 > shownum) {
                    let totst = this.commonFunc.showToast({
                      message: "恭喜您获得：" + data.data.thing + " 分",
                      duration: 3000,
                      position: "middle"
                    });
                    totst.present();
                    this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                  } else {
                    alert.present();
                  }

                }
                if (data.data.eventAction == 3) {
                  if (data.data.ext1 > shownum) {
                    this.commonFunc.openUrl(data.data.thing);
                    this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                  } else {
                    alert.present();
                  }
                }
                if (data.data.eventAction == 4) {
                  if (data.data.ext1 > shownum) {
                    this.navCtrl.push(data.data.thing, {
                      themeData: data.data,
                      elevatorNum: this.elevatorNum
                    });
                  } else {
                    alert.present();
                  }
                }
              }, error => {
                alert.present();
              })
            } else {
              alert.present();
            }
          });
        } else {
          load.dismiss();
          let totst = this.commonFunc.showToast({
            message: "提交失败",
            duration: 2000,
            position: "top"
          });
          totst.present();
        }
      });
    }
    if (
      this.navParams.get("ElevaType") == "4" ||
      this.navParams.get("ElevaType") == "6"
    ) {
      this.getTips();
      this.param["escalatorHalfMon"].maintenanceUserId2 = this.maintUser2;
      if (this.userList) {
        this.userList.forEach(element => {
          if (element.id = this.maintUser2) {
            this.param["escalatorHalfMon"].maintenanceUserName2 = element.userName;
          }
        });
      }
      this.endTime = this.param["escalatorHalfMon"].maintenanceEndDate;
      this.param[
        "escalatorHalfMon"
      ].maintenanceUserSignId1 = this.maintenanceUserSignId1;
      this.param[
        "escalatorHalfMon"
      ].maintenanceUserSignId2 = this.maintenanceUserSignId2;
      this.httpService.addEscalator(this.param).subscribe(data => {
        if (data.status == 200) {
          this.storage.set(this.elevatorNum + "escalator-half-mon", null);
          this.storage.set(this.elevatorNum + "escalator-half-year", null);
          this.storage.set(this.elevatorNum + "escalator-quarter", null);
          this.storage.set(this.elevatorNum + "escalator-year", null);
          this.storage.set(this.elevatorNum + "work-sum",null);
          this.storage.set(this.elevatorNum + "safeImg",null);
          load.dismiss();
          this.storage.get(this.userId + 'cache').then(datalist2 => {
            if (datalist2) {
              let datalist: [any] = Object.assign([], datalist2);
              for (let i = 0; i < datalist.length; i++) {
                let element = datalist[i]
                if (element.dateTime == this.endTime) {
                  datalist.splice(i, 1);
                }
              }
              this.storage.set(this.userId + 'cache', datalist);
            }
          });
          this.navCtrl.setRoot(HomePage).then(() => {
            window["jQuery"]("#signatureid").remove();
            window["jQuery"]("#showImage").remove();
            window["jQuery"](`#` + this.time1).remove();
            window["jQuery"](`#` + this.time2).remove();
            //场景埋点
            if (data.data && data.data.status == 1) {
              this.storage.get(data.data.ext2 + this.userId + this.elevatorNum).then(dt => {
                let shownum: number = 0;
                if (dt) {
                  shownum = dt.shownum;
                }

                if (data.data.eventAction == 1) { //nothing to do
                  alert.present();
                  this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                }
                if (data.data.eventAction == 2) {
                  if (data.data.ext1 > shownum) {
                    let totst = this.commonFunc.showToast({
                      message: "恭喜您获得：" + data.data.thing + " 分",
                      duration: 3000,
                      position: "middle"
                    });
                    totst.present();
                    this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                  } else {
                    alert.present();
                  }

                }
                if (data.data.eventAction == 3) {
                  if (data.data.ext1 > shownum) {
                    this.commonFunc.openUrl(data.data.thing);
                    this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                  } else {
                    alert.present();
                  }
                }
                if (data.data.eventAction == 4) {
                  if (data.data.ext1 > shownum) {
                    this.navCtrl.push(data.data.thing, {
                      themeData: data.data,
                      elevatorNum: this.elevatorNum
                    });
                  } else {
                    alert.present();
                  }
                }
              }, error => {
                alert.present();
              })
            } else {
              alert.present();
            }
          });
        } else {
          load.dismiss();
          let totst = this.commonFunc.showToast({
            message: "提交失败",
            duration: 2000,
            position: "top"
          });
          totst.present();
        }
      });
    }
    //日常巡检
    if (this.ElevaType == "daily") {
      this.param["signPicUrl"] = this.maintenanceUserSignId1;
      this.httpService.inspectionAdd(this.param).subscribe(data => {
        if (data.status == 200) {
          load.dismiss();
          this.navCtrl.setRoot(HomePage).then(() => {
            window["jQuery"]("#signatureid").remove();
            window["jQuery"]("#showImage").remove();
            window["jQuery"](`#` + this.time1).remove();
            window["jQuery"](`#` + this.time2).remove();
            //场景埋点
            if (data.data && data.data.status == 1) {
              this.storage.get(data.data.ext2 + this.userId + this.elevatorNum).then(dt => {
                let shownum: number = 0;
                if (dt) {
                  shownum = dt.shownum;
                }

                if (data.data.eventAction == 1) { //nothing to do
                  alert.present();
                  this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                }
                if (data.data.eventAction == 2) {
                  if (data.data.ext1 > shownum) {
                    let totst = this.commonFunc.showToast({
                      message: "恭喜您获得：" + data.data.thing + " 分",
                      duration: 3000,
                      position: "middle"
                    });
                    totst.present();
                    this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                  } else {
                    alert.present();
                  }

                }
                if (data.data.eventAction == 3) {
                  if (data.data.ext1 > shownum) {
                    this.commonFunc.openUrl(data.data.thing);
                    this.storage.set(data.data.ext2 + this.userId + this.elevatorNum, { shownum: ++shownum });
                  } else {
                    alert.present();
                  }
                }
                if (data.data.eventAction == 4) {
                  if (data.data.ext1 > shownum) {
                    this.navCtrl.push(data.data.thing, {
                      themeData: data.data,
                      elevatorNum: this.elevatorNum
                    });
                  } else {
                    alert.present();
                  }
                }
              }, error => {
                alert.present();
              })
            } else {
              alert.present();
            }
          });
        } else {
          load.dismiss();
          if ((data.message + "").indexOf("Field 'ID'") > 0) {
            this.navCtrl.setRoot(HomePage).then(() => {
              window["jQuery"]("#signatureid").remove();
              window["jQuery"]("#showImage").remove();
              window["jQuery"](`#` + this.time1).remove();
              window["jQuery"](`#` + this.time2).remove();
            });
          } else {
            let totst = this.commonFunc.showToast({
              message: "提交失败",
              duration: 2000,
              position: "top"
            });
            totst.present();
          }
        }
      });
    }
    //维保通知签名
    if (this.navParams.get("ElevaType") == "notice") {
      let paras: any = {};
      paras["id"] = this.param.id;
      paras["safeUserName"] = this.param.receiveUserName;
      paras["usedPartyOpt"] = this.suggest;
      paras["elevatoriMonId"] = this.param.elevatoriMonId;
      paras["elevatoriType"] = this.param.elevatoriType;
      paras["safeUserSignId2"] = this.maintenanceUserSignId1;
      paras["safeUserSignTime"] = moment(new Date()).format("YYYYMMDDHHmmss");
      paras["status"] = "0"; //0没问题 1有问题
      paras["isRead"] = "1"; //0未读 1已读
      // elevatoriMonId
      // elevatoriType
      // safeUserSignId2
      // safeUserName
      // safeUserSignTime
      // status
      // elevatoriMonId
      this.httpService.updateReadState(paras).subscribe(data => {
        if (data.status == 200) {
          load.dismiss();
          alert.present();
          this.navCtrl.setRoot(HomePage).then(() => {
            window["jQuery"]("#signatureid").remove();
            window["jQuery"]("#showImage").remove();
            window["jQuery"](`#` + this.time1).remove();
            window["jQuery"](`#` + this.time2).remove();
            this.storage.set(this.elevatorNum + '-his-' + "escalator-half-mon", null);
            this.storage.set(this.elevatorNum + '-his-' + "escalator-half-year", null);
            this.storage.set(this.elevatorNum + '-his-' + "escalator-quarter", null);
            this.storage.set(this.elevatorNum + '-his-' + "escalator-year", null);
            this.storage.set(this.elevatorNum + '-his-' + "debris-half", null);
            this.storage.set(this.elevatorNum + '-his-' + "debris-half-year", null);
            this.storage.set(this.elevatorNum + '-his-' + "debris-quarter", null);
            this.storage.set(this.elevatorNum + '-his-' + "debris-year", null);
            this.storage.set(this.elevatorNum + '-his-' + "hydpress-half", null);
            this.storage.set(this.elevatorNum + '-his-' + "hydpress-half-year", null);
            this.storage.set(this.elevatorNum + '-his-' + "hydpress-quarter", null);
            this.storage.set(this.elevatorNum + '-his-' + "hydpress-year", null);
            this.storage.set(this.elevatorNum + '-his-' + "traction-half", null);
            this.storage.set(this.elevatorNum + '-his-' + "traction-half-year", null);
            this.storage.set(this.elevatorNum + '-his-' + "traction-quarter", null);
            this.storage.set(this.elevatorNum + '-his-' + "traction-year", null);
          });
        } else {
          load.dismiss();
          let totst = this.commonFunc.showToast({
            message: "提交失败",
            duration: 2000,
            position: "top"
          });
          totst.present();
        }
      });
    }
  }

  /**
   * 弹出维保工作时间提示
   * @param param
   */
  getTips(){
    //使用单位查看维保记录时，无此参数
    let maintenancenUsedTime : string;
    if(this.param["tractionHalfMon"] == undefined) {
      this.httpService
          .queryTractionDetails(this.param.elevatoriMonId)
          .subscribe(dd =>{

            maintenancenUsedTime = dd.data.traction.tractionHalfMon.maintenancenUsedTime;


            const toast = this.toastCtrl.create({
                message: "本次维保工作时间："+ maintenancenUsedTime +"分钟",
                duration: 2000,
                position: "top"
            });
            toast.present();
          });
    }else{
      maintenancenUsedTime = this.param["tractionHalfMon"].maintenancenUsedTime;
        const toast = this.toastCtrl.create({
            message: "本次维保工作时间："+ maintenancenUsedTime +"分钟",
            duration: 2000,
            position: "top"
        });
        toast.present();
    }

  }
  //签名
  getImgUrl1(param) {
    this.maintenanceUserSignId1 = param.imageDate;
  }

  getImgUrl2(param) {
    this.maintenanceUserSignId2 = param.imageDate;
  }

  gotoMaint() {
    if (this.param.elevatoriType == "1" || this.param.elevatoriType == "5") {
      this.navCtrl.push(TractionPage, {
        elevatorNum: this.elevatorNum,
        ElevaType: this.param.elevatoriType,
        type: this.maitType,
        isLook: true
      });
    }
    if (this.param.elevatoriType == "2") {
      this.navCtrl.push(HydpressPage, {
        elevatorNum: this.elevatorNum,
        ElevaType: this.param.elevatoriType,
        type: this.maitType,
        isLook: true
      });
    }
    if (this.param.elevatoriType == "3") {
      this.navCtrl.push(DebrisPage, {
        elevatorNum: this.elevatorNum,
        ElevaType: this.param.elevatoriType,
        type: this.maitType,
        isLook: true
      });
    }
    if (this.param.elevatoriType == "4" || this.param.elevatoriType == "6") {
      this.navCtrl.push(EscalatorPage, {
        elevatorNum: this.elevatorNum,
        ElevaType: this.param.elevatoriType,
        type: this.maitType,
        isLook: true
      });
    }
  }
}
